<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="姓名/手机号" prop="user_name">
        <el-input v-model="queryParams.user_name" placeholder="请输入姓名/手机号" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="担保编号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入担保编号"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同号" prop="contract_code">
        <el-input
          v-model="queryParams.contract_code"
          placeholder="请输入合同号"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="风险分类" prop="risk">
        <el-select v-model="queryParams.risk" placeholder="风险分类" clearable style="width: 200px">
          <el-option v-for="dict in riskList" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['system:post:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="担保编号" align="center" prop="id" /> -->
      <el-table-column label="姓名" align="center" prop="user_name" />
      <!-- <el-table-column
        label="合同号"
        align="center"
        prop="contract_code"
        width="180"
      /> -->
      <!-- <el-table-column label="当前期次" align="center" prop="stages_phase" /> -->
      <el-table-column label="逾期本金" align="center" prop="ctd_principal_amt" />
      <el-table-column label="逾期利息" align="center" prop="ctd_interesta_amt" />
      <!-- <el-table-column
        label="担保管理费"
        align="center"
        width="160"
        prop="manage_amt"
      /> -->
      <el-table-column label="担保类型" align="center">
        <template #default="scope">
          <span>{{ loanTypes(scope.row.loan_type) }}</span
          ><br />
          <el-text class="mx-1" type="danger">已逾期</el-text>
        </template>
      </el-table-column>
      <el-table-column label="本期还款日" align="center" width="160" prop="pmt_due_date"> </el-table-column>
      <el-table-column label="逾期天数" align="center" prop="overdue_days" />
      <el-table-column label="风险分类" align="center" prop="risk" />
      <el-table-column label="催收次数" align="center" prop="num">
        <template #default="scope">
          <el-link type="primary" @click="handelRecord(scope.row)" v-hasPermi="['system:post:edit']">{{ scope.row.postSort8 }}</el-link>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="180" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-tooltip content="查看" placement="top">
            <el-button link type="primary" icon="View" @click="handelDetail(scope.row)" v-hasPermi="['system:post:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="催收待办" placement="top">
            <el-button link type="primary" icon="Finished" @click="handleCollection(scope.row)" v-hasPermi="['system:post:remove']"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page_size" v-model:limit="queryParams.page_count" @pagination="getList" />

    <!-- 到期提醒 -->
    <el-dialog :title="'催收处理(客户 ' + item.user_name + ')'" v-model="open" width="640px" append-to-body>
      <el-form ref="postRef" :model="form" :rules="rules" :label-width="form.urge_method == 2 ? 140 : 80">
        <el-form-item label="催收方式" prop="urge_method">
          <el-radio-group v-model="form.urge_method">
            <el-radio :label="1">电话催收</el-radio>
            <el-radio :label="2">上门/外访</el-radio>
            <!-- <el-radio label="3">诉松</el-radio> -->
          </el-radio-group>
        </el-form-item>

        <template v-if="form.urge_method == 1">
          <el-form-item label="催收时间" prop="urge_time">
            <el-date-picker v-model="form.urge_time" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择" />
          </el-form-item>
          <el-form-item label="是否接通" prop="phone_connected">
            <el-radio-group v-model="form.phone_connected">
              <el-radio label="0">否</el-radio>
              <el-radio label="1">是</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="接听对象" prop="phone_recipient">
            <el-radio-group v-model="form.phone_recipient">
              <el-radio label="1">本人</el-radio>
              <el-radio label="2">非本人</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="催收反馈" prop="feedback">
            <el-input v-model="form.feedback" :rows="2" type="textarea" placeholder="请填写担保人的应答信息，真实反馈不得违背客观事实。" />
            <FileUpload style="padding-top: 12px" title="上传录音" :fileType="['mp3']" ref="recordUpload"></FileUpload>
          </el-form-item>
        </template>
        <template v-if="form.urge_method == 2">
          <el-form-item label="催收时间" prop="urge_time">
            <el-date-picker v-model="form.urge_time" type="datetime" placeholder="请选择" value-format="YYYY-MM-DD HH:mm:ss" />
          </el-form-item>
          <el-form-item label="是否见到担保人" prop="visit_see">
            <el-radio-group v-model="form.visit_see">
              <el-radio label="0">否</el-radio>
              <el-radio label="1">是</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="未见到担保人原因" prop="visit_reason">
            <el-radio-group v-model="form.visit_reason">
              <el-radio label="1">地址错误</el-radio>
              <el-radio label="2">担保人不在</el-radio>
              <el-radio label="3">见到家属</el-radio>
              <el-radio label="4">其他</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="其他原因" prop="visit_other">
            <el-input v-model="form.visit_other" :rows="2" type="textarea" placeholder="请填写其他原因" />
          </el-form-item>
          <el-form-item label="催收人是否签字" prop="visit_sign">
            <el-radio-group v-model="form.visit_sign">
              <el-radio label="1">是</el-radio>
              <el-radio label="2">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="催收反馈" prop="feedback">
            <el-input v-model="form.feedback" :rows="2" type="textarea" placeholder="请填写担保人的应答信息，真实反馈不得违背客观事实。" />

            <FileUpload style="padding-top: 12px" title="上传外访附件" ref="fileUploadRefs"></FileUpload>
            <ImageUpload v-model="imageList" />
          </el-form-item>
        </template>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 即将到期提醒详情 -->
    <el-dialog :title="preview.title" v-model="preview.open" width="60%" top="100vh" append-to-body class="scrollbar">
      <el-table :data="preview.data" style="width: 100%">
        <el-table-column prop="customer" label="客户" />
        <el-table-column prop="time" label="提醒时间" />
        <el-table-column prop="number" label="次数" />
        <el-table-column prop="style" label="提醒方式" />
      </el-table>
      <div class="dialog-css"></div>
    </el-dialog>
  </div>
</template>

<script setup name="Expire">
import { methodType, loanTypes } from "@/utils/dict";
import { list, collection } from "@/api/collection/expire";
const { proxy } = getCurrentInstance();
const riskList = ref([
  { label: "M1", value: "M1" },
  { label: "M2", value: "M2" },
  { label: "M3", value: "M3" },
  { label: "M4", value: "M4" },
  { label: "M5", value: "M5" },
  { label: "M6", value: "M6" },
]);

const postList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const router = useRouter();
const imageList = ref([]);
const preview = ref({});
const item = ref({});

const data = reactive({
  form: {
    urge_method: 1,
    urge_time: "",
    phone_connected: "",
    phone_recipient: "",
    visit_see: "",
    visit_sign: "",
    visit_reason: "",
    visit_other: "",
  },
  queryParams: {
    page_count: 10,
    page_size: 1,
    id: "",
    contract_code: "",
    risk: "",
    user_name: "",
  },
  rules: {
    postName: [{ required: true, message: "名称不能为空", trigger: "blur" }],
    postCode: [{ required: true, message: "编码不能为空", trigger: "blur" }],
    postSort: [{ required: true, message: "顺序不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询列表 */
function getList() {
  loading.value = true;
  list({ ...queryParams.value, user_phone: queryParams.value.user_name }).then((response) => {
    postList.value = response.data.list.slice(0, 3);
    total.value = response.data.total_page;
    loading.value = false;
    postList.value[0].user_name = "王福";
    postList.value[1].user_name = "李海";
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
// 即将到期提醒详情
function handelRecord() {
  preview.value.open = true;
}
/** 表单重置 */
function reset() {
  form.value = {
    urge_method: 1,
    urge_time: "",
    phone_connected: "",
    phone_recipient: "",
    visit_see: "",
    visit_sign: "",
    visit_reason: "",
    visit_other: "",
  };
  proxy.resetForm("postRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page_count = 10;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.postId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 催收代办 */
function handleCollection(value) {
  item.value = value;
  reset();
  imageList.value = [];
  open.value = true;
}
/** 修改按钮操作 */
function handelDetail(row) {
  router.push({
    path: "/collection/expire-detail",
    state: {
      infos: {
        id: row.id,
        apply_id: row.apply_id,
        purpose_name: row.purpose_name,
        debit_card_id: row.debit_card_id,
        user_name: row.user_name,
        approval_rate: row.approval_rate,
        loan_type_classify: loanTypes(row.loan_type).slice(0, 2),
        loan_type_name: loanTypes(row.loan_type).slice(2, 5),
        loan_stages: row.loan_stages + "个月",
        apply_quota: !row.apply_quota ? row.apply_quota : row.apply_quota + "元",
        method_type: methodType(row.method),
        contract_amount: !row.contract_amount ? row.contract_amount : row.contract_amount + "元",
        approval_amount: !row.approval_amount ? row.approval_amount : row.approval_amount + "元",
      },
    },
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["postRef"].validate((valid) => {
    let fileInfo = form.value.urge_method == 1 ? proxy.$refs.recordUpload.getFileInfo() : proxy.$refs.fileUploadRefs.getFileInfo();

    if (valid) {
      collection({
        apply_id: item.value.apply_id,
        detail_id: item.value.id,
        attachment_name: fileInfo.fileName,
        attachment_path: fileInfo.filePath,
        pic_path: imageList.value.map((item) => item.file_path).join(","),
        ...form.value,
      }).then((response) => {
        proxy.$modal.msgSuccess("修改成功");
        open.value = false;
        getList();
      });
    }
  });
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download("loan/export/overdueList", {
    ...queryParams.value,
    user_phone: queryParams.value.user_name,
  });
}

getList();
</script>
